package com.own.component.util.excel.util.write.record;

import com.own.component.util.excel.exception.ExcelException;
import com.own.component.util.excel.model.Coordinate;
import com.own.component.util.excel.util.write.model.Freeze;
import com.own.component.util.excel.util.write.model.Merge;
import com.own.component.util.excel.util.write.model.Style;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SheetRecord
 *
 * @author chenxueli
 * @date 2022-06-15 03:14:15
 */
@Data
public class SheetRecord {

    /**
     * 数据列表
     */
    private final List<CellRecord> list;
    /**
     * 合并的单元格列表
     */
    private final List<Merge> mergeList;
    /**
     * 高度map
     */
    private final Map<Integer, Short> heightMap;
    /**
     * 宽度map
     */
    private final Map<Integer, Integer> widthMap;
    /**
     * 样式map
     */
    private final Map<String, Style> styleMap;
    /**
     * 工作表标题
     */
    private final String title;
    /**
     * 已经写入的单元格
     */
    private final Map<String, Integer> writeMap;
    /**
     * 原点坐标
     */
    private final Coordinate origin;
    /**
     * 当前的坐标信息
     */
    private Coordinate current;
    /**
     * 操作的最大坐标信息
     */
    private Coordinate max;
    /**
     * 默认的样式
     */
    private Style defaultStyle;
    /**
     * 默认的高度
     */
    private Short defaultHeight;
    /**
     * 默认的宽度
     */
    private Integer defaultWidth;
    /**
     * 冻结的单元格信息
     */
    private Freeze freeze;

    public SheetRecord() {
        this(null);
    }

    public SheetRecord(String title) {
        this(title, 1, 1);
    }

    public SheetRecord(String title, int x, int y) {
        this.title = StringUtils.isBlank(title) ? ("sheet" + System.currentTimeMillis()) : title;
        this.list = new ArrayList<>();
        this.mergeList = new ArrayList<>();
        this.heightMap = new HashMap<>(32);
        this.widthMap = new HashMap<>(32);
        this.writeMap = new HashMap<>(32);
        this.styleMap = new HashMap<>(32);
        this.current = new Coordinate();
        this.max = new Coordinate();
        this.origin = new Coordinate(Math.max(x, 1), Math.max(y, 1));
        this.defaultStyle = null;
        this.defaultHeight = null;
        this.defaultWidth = null;
    }

    public int getX() {
        return current.getX();
    }

    public int getY() {
        return current.getY();
    }

    /**
     * 获取单元格数据
     *
     * @param x x坐标
     * @param y y坐标
     * @return 单元格数据
     */
    public CellRecord getItem(int x, int y) {
        var key = Coordinate.buildKey(x, y);
        if (writeMap.containsKey(key)) {
            setCoordinate(x, y);
            return this.list.get(writeMap.get(key));
        }
        throw new ExcelException("单元格获取错误");
    }

    /**
     * 设置坐标信息
     *
     * @param x x坐标
     * @param y y坐标
     */
    public void addCoordinate(int x, int y) {
        current.refresh(x, y);
        calcMax();
    }

    /**
     * 设置坐标信息
     *
     * @param x x坐标
     * @param y y坐标
     */
    public void setCoordinate(int x, int y) {
        current.refresh(x, y);
        calcMax();
    }

    /**
     * 刷新最大坐标
     */
    public void calcMax() {
        this.max.setMax(this.current);
    }

}
